<template>
  <div class="clinicPayment">
      <!-- clinicPayment -->
      <header>
          <Row>
              <Col>
                  <div class="margin-up-down">
                      <span>开始日期</span>
                      <DatePicker type="date" v-model="getParams.startDate" style='width:120px;'></DatePicker>
                  </div>
                  <div class="margin-up-down">
                      <span>结束日期</span>
                      <DatePicker type="date" v-model="getParams.endDate" style='width:120px;'></DatePicker>
                  </div>
                  <div class="margin-up-down">
                      <Button type='success' @click='loadingCardRechargeList(1)'>查询</Button>
                  </div>
              </Col>
          </Row>
      </header>
      <Table stripe :row-class-name="rowClassName" :columns="columns1" :data="data1" style='margin:10px 0;'></Table>
      <div class="page">
<!--            <Page :total="count" :current='pageNo' :page-size="pageSize" show-total show-elevator @on-change="pageChange"/>-->
      </div>
  </div>
</template>
<script>
import api from "@/api/commonApi";
import { DatePicker, TimePicker, Card, Progress } from 'view-design'
// 诊间交费
export default {
  components: {
      DatePicker,
      TimePicker,
      Card,
      Progress
  },
  data () {
      return {
        ORDER_TYPE_ENUM:[
          {code:0,name:'健康咨询'},
          {code:1,name:'巡诊'},
          {code:2,name:'复诊'},
          {code:3,name:'续方'},
          {code:4,name:'用药咨询'},
          {code:5,name:'购药'},
          {code:6,name:'预约挂号'},
          {code:7,name:'家医签约'},
          {code:8,name:'远程门诊'},
          {code:9,name:'直播'},
          {code:10,name:'点播'},
          {code:11,name:'诊间缴费'},
          {code:12,name:'充值'},
          {code:13,name:'住院押金缴费'},
          {code:14,name:'院后随访'},
          {code:15,name:'外延处方'},
          {code:16,name:'远程会诊'},
          {code:17,name:'病历复印'},
          {code:18,name:'检查检验预约'},
          {code:19,name:'检查预约'},
          {code:20,name:'检验预约'},
          {code:21,name:'体检预约'},
          {code:22,name:'互联网缴费'},
          {code:23,name:'手术预约'},
          {code:30,name:'线上就诊'}
        ],

        // 头部参数
          getParams: {
              // 开始时间
              startDate: '',
              startTime: "00:00:00",
              // 结束时间
              endDate: "",
              endTime: "23:59:59",
              // 查询条件
              searchKey:"",
              // 异常
              single : false,
          },
          // 列表数据
          columns1: [
              {
                  key: "isum",
                  title: "排序",
                  align: "center",
                  width:60,
                  render: (h, params) => {
                      let isum = (this.pageNo-1)*this.pageSize + params.index +1;
                      return h("span", isum);
                  }
              },
              {
                  key: "channels",
                  title: "支付渠道",
                  align: "center",
                  render:(h, params) => {
                        let checked = params.row;
                        let index = Number(checked.channels)
                        let name = this.Channels[index]
                        return [
                            h("span", name)
                        ]
                    }
              },
              {
                  key: "itemType",
                  title: "类型",
                  align: "center",
                  render:(h, params) => {
                        let checked = params.row;
                        let index = Number(checked.itemType)
                        let name = this.jiaoyiTypes[index]
                        return [
                            h("span", name)
                        ]
                  }
              },
              {
                  key: "count",
                  title: "单数",
                  align: "center"

              },
              {
                  key: "amount",
                  title: "金额",
                  align: "center"

              }
          ],
          data1: [],
          // 分页数据
          count: 0,
          pageNo: 1,
          pageSize: 10,
          // 弹窗状态
          clinicStatus: false,
          // 单个平台状态列表
          statusList: ['失败','交易成功','退费', '未获取到状态'],
          //支付渠道
          Channels:['微信','支付宝','线下支付','就诊卡号'],
          //支付渠道
          jiaoyiTypes:['退款','付款','提现'],
          // 点击来源
          buttonStatus: 1,
          orderStatusList: [
              {
                  name: "支付成功",
                  id:'1'
              },
              {
                  name: "未支付",
                  id:'2'
              },
              {
                  name: "支付失败",
                  id:'3'
              },
              {
                  name: "支付取消",
                  id:'4'
              },
              {
                  name: "已退费",
                  id:'5'
              },
          ],
          // 订单对账状态
          manualParams: {
              // 平台状态
              PlatformStatus: '已支付',
              // 互联网平台状态
              interPlatformStatus: "",
              // HIS状态
              hisPlatformStatus: "",
              // 弹窗查询流水号
              modalSearchKey:"",
              // id
              transId:""
          },
          stautParams: ['未对账','对账成功','未获取到数据','金额不符','状态不符'],
          // // 自动对账状态
          autoLoad:false,
          // 进度条
          progressStart: 0,
          uuid :'',
          DZdisabled: false,
      }
  },
  created () {
      let breadList = [
          { path: "/index", title: "首页" },
          {
              path: "/index/operation/clinicPayment",
              title: "互联网医院"
          },
          {
              path: "/index/internetHospital/clinicPayment/list",
              title: "诊间缴费对账"
          }
      ];
      this.getParams.startDate = this.GetDate(0);
      this.getParams.endDate = this.GetDate(0);
      let date = new Date();
      this.getParams.endTime = date.getHours() + ':' + date.getMinutes()
      this.$emit("changeBreadList", breadList);
  },
  mounted () {
      this.loadingCardRechargeList(this.pageNo)
  },
  methods: {
      findTypeName(h, params) {
        let record = params.row;
        console.log("params===============",params)
        let typeName = "";
        //找到名字
        for(let i=0;i<this.ORDER_TYPE_ENUM.length;i++){
          if( this.ORDER_TYPE_ENUM[i].code==record.itemType){
            typeName = this.ORDER_TYPE_ENUM[i].name;
            break;
          }
        }
        if(typeName==""){
          typeName = "未知类型-"+record.businessType;
        }
        return [
          h("span", typeName)
        ]
      },
      query () {
        let url = api.statFinanceDoctor;
        let param = {
          startTime: this.getData(this.getParams.startDate)
          // hospitalId: sessionStorage.getItem("hospitalId")
        }
        this.$axios.post(url, param).then(resp => {
          this.data1 = resp.data.object;
        })
      },
      // 分页器改变
      pageChange (index) {
          this.pageNo = index;
          // this.loadingCardRechargeList(index)
          this.query(index)
      },
      // 自动对账确定按钮
      ok () {
          this.autoLoad = true
          this.autorecon();
      },
      // 请求进度条
      progressbord () {
          let url = api.speedofprogress;
          let params = {
              uuid: this.uuid
          }
          this.$axios.post(url, params).then(res => {
              if(res.data.success) {
                  let ret = res.data.object;
                  console.log("进度条", ret);
                  let err = Boolean(ret.ERROR_COUNT) ? Number(ret.ERROR_COUNT) : 0
                  let start = Number(ret.EXECUTE_START)
                  let count = Boolean(ret.EXECUTE_COUNT) ? Number(ret.EXECUTE_COUNT) : 0
                  let currentTotal = err + count
                  console.log('currentTotal: ', currentTotal);

                  // 进度条参数
                  let progressEnd = Number(ret.TOTAL_COUNT) || 0
                  console.log('progressEnd: ', progressEnd);
                  let progressStart = parseInt((currentTotal * 100) / progressEnd);
                  console.log('progressStart: ', progressStart);

                  this.progressStart = progressStart
                  if(start != 1) {
                      return ""
                  }
                  // 当已对账数量+异常数据 = 总数的时候停止递归然后调用取消对账接口
                  if(currentTotal == ret.TOTAL_COUNT) {
                      this.$axios.post(api.deleteredis, {
                          uuid: this.uuid
                      }).then(res => {
                          if (res.data.success) {
                              this.clinicStatus = false;
                              this.autoLoad = false;
                              this.progressStart = 100
                              this.loadingCardRechargeList(this.pageNo);
                              this.$Message.success("对账成功")
                          }
                      })
                      return ""
                      // 不等于总数时继续递归调用进度条接口
                  } else if(currentTotal != ret.TOTAL_COUNT) {
                      console.log("继续对账");
                      if (this.autoLoad) {
                          this.progressbord();
                      }
                  }
              }
          })
      },
      // 点击弹窗取消,初始化弹窗里的值
      cancel () {
          this.clinicStatus = false
          //支付状态
          this.manualParams.PlatformStatus = ''
          this.manualParams.interPlatformStatus = ''
          this.manualParams.hisPlatformStatus = ''
          this.manualParams.modalSearchKey = ''
      },
      // 取消对账
      cancelPayment () {
          if(!this.uuid) {
              this.clinicStatus = false;
              return ""
          }
          this.$axios.post(api.deleteredis, {
              uuid: this.uuid
          }).then(res => {
              if (res.data.success) {
                  this.$Message.success ("取消对账成功");
                  this.clinicStatus = false
                  this.autoLoad = false
              }
          })
      },
      // 对账功能逻辑
      cliniction (status, row) {
          if(this.getParams.startDate === '' || this.getParams.startDate === null){
              this.$Message.error("请选择对账开始时间")
          }else{
              this.buttonStatus = status
              this.DZdisabled = false
              this.clinicStatus = true
              if(status == 2) {
                  console.log('row', row);
                  this.manualParams.PlatformStatus = row.checkingStatus == 1 ? '已对账' : "未对账";
                  // 互联网平台状态
                  this.manualParams.interPlatformStatus = row.platformThridStatus
                  // HIS状态
                  this.manualParams.hisPlatformStatus = row.platformHisStatus
                  // id
                  this.manualParams.transId = row.transId
                  console.log(this.manualParams);
              }
          }
      },
      //更改平台状态
      changePlatform () {
          console.log('更改平台状态');
          let url = api.updatestatus
          let params = {}
          return ""
          this.$axios.post(url, params).then(res => {
              if (res.data.success) {
                  // this.$Message.info('由于对账信息较多,请等待5-10分钟重新查看!');
              } else {
                  this.$Message.error("更改平台状态失败")
              }
              this.clinicStatus = false
          })
      },
      // 查询账单流水号
      searchSerialNumber () {
          let url = api.querybyserialnumber;
          let params = {
              serialNumber: this.manualParams.modalSearchKey
          }
          console.log(params);
          // return ""
          this.$axios.post(url,params).then(res => {
              if(res.data.success) {
                  let ret = res.data.object;
                  this.cliniction(2,ret)
                  console.log(ret);
              }
          })
      },
      //更改His平台状态
      changeHisStatus () {
          console.log('更改His平台状态');
          let url = ''
          let params = {}
          return ""
          this.$axios.post(url, params).then(res => {
              if (res.data.success) {

              } else {
                  this.$Message.error("更改His平台状态失败")
              }
              this.clinicStatus = false
          })
      },
      // 加载页面列表数据
      loadingCardRechargeList (pageNo) {
          /*
              加载页面列表对账信息
          */
          this.pageNo = pageNo
          let url = api.incomeAndOutSelect
          let startTime = this.getData(this.getParams.startDate) + ' ' + this.getParams.startTime;
          let endTime = this.getData(this.getParams.endDate) + ' ' + this.getParams.endTime;
          let query = this.getParams.searchKey.trim();
          let params = {
            //   pageNo: pageNo,
              pageSize: this.pageSize,
              startTime: startTime ? startTime : '',
              endTime: endTime ? endTime : '',
              status: Number(this.getParams.single),
              businessSources: 11,
          }
          if (Boolean(this.getParams.searchKey)) {
              params.query = this.getParams.searchKey.trim();
          } else {
              params.query = ''
          }
          if(this.getParams.startDate === '' || this.getParams.startDate === null){
              delete params.startTime
          }
          if(this.getParams.endDate === '' || this.getParams.endDate === null){
              delete params.endTime
          }
          console.log('诊间交费参数', params);
          this.$axios.post(url,params).then(ress => {
              if(ress.data.success) {
                  let ret = ress.data.object;
                  this.count = ret.total
                  console.log(ret);
                  if(ret.records){
                      ret.records.forEach((item, index) => {
                          item.isum = this.addZeros(index)
                      })
                      this.data1 = ret.records
                  } else {
                      this.data1 = []
                  }
              } else {
                  this.$Message.info("请确认当前账号是否绑定机构")
              }
          })
      },
      // 显示异常数据
      rowClassName (row, index) {
          if(Number(row.checkingStatus) == 0) {
              return ''
          } else if (row.tradingChannels >= 0) {
              // console.log('row.tradingChannels', row.tradingChannels)
              if(row.tradingChannels == 3) {
                  // 当支付渠道为就诊卡时只使用 支付平台与His对账的状态区分显示样式
                  if(row.platformHisStatus != 1) {
                      return 'demo-error-row';
                  } else {
                      return 'demo-success-row'
                  }
                  // 支付渠道为其他状态时 使用三个对比状态进行区分显示样式
              } else if(row.platformHisStatus != 1 || row.thirdHisStatus != 1 || row.platformThirdStatus != 1) {
                  console.log('hhhhhhhhhhhhhh')
                  return 'demo-error-row';
              }
          } else if(Number(row.checkingStatus) == 1) {
              return 'demo-success-row'
          }
          return '';
      },
      // 转换日期格式
      getData(data){
          let datas = new Date(data)
          var curr_date = datas.getDate();
          var curr_month = datas.getMonth() + 1;
          var curr_year = datas.getFullYear();
          function add(num){
              if(Number(num)<10) {
                  return '0' + num
              }
              return num
          }
          return curr_year + "-" + add(curr_month)+'-' + add(curr_date)
      }
  }
};
</script>

<style lang="less" scoped>
.clinicPayment {
  width: calc(100%);
  padding: 10px 30px;
  margin: 0 auto;
  background: #fff;
  .margin-up-down {
      display: inline-block;
      margin-top: 10px;
  }
  .page{
      text-align: center;
      margin-top: 20px;
  }
}
</style>
<style>
.ivu-table .demo-error-row td{
  color: red;
}
.ivu-table .demo-error-row td a{
  color:red
}
.ivu-table .demo-success-row td{
  color: #2b85e4;
}
.ivu-table .demo-success-row td a{
  color:#2b85e4
}
</style>
